package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.constants.enums.Status;
import com.fortune.dto.excution.ClientRepaymentExcution;
import com.fortune.dto.in.clientRepayment.ClientRepaymentConfirm;
import com.fortune.dto.in.clientRepayment.ClientRepaymentDetails;
import com.fortune.dto.in.clientRepayment.ClientRepaymentList;
import com.fortune.dto.in.clientRepayment.piComfirm;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.ClientRepaymentEnum;
import com.fortune.service.COrderBaseService;
import com.fortune.vo.clientRepayment.ClientRepaymentDetailsVo;
import com.fortune.vo.clientRepayment.ClientRepaymentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Api(value = "clientRepayment", description = "客户回款管理", tags = {"客户回款管理"})
@RestController
@RequestMapping("clientRepayment")
@Validated
public class ClientRepaymentController {
    @Autowired
    private COrderBaseService cOrderBaseService;

    @ApiOperation(value = "客户回款列表", nickname = "clientRepaymentList", notes = "", tags={ "@石匡" })
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
            @ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
            @ApiImplicitParam(name="driver", value="司机",paramType="query",dataType="string"),
            @ApiImplicitParam(name="pay", value="是否付款(YES,NO)",paramType="query",dataType="string",allowableValues = "YES,NO,ALL"),
            @ApiImplicitParam(name="payType", value="付款方式",paramType="query",dataType="string"),
            @ApiImplicitParam(name="delivery", value="是否已送达(YES,NO)",paramType="query",dataType="string",allowableValues = "YES,NO,ALL"),
            @ApiImplicitParam(name="boxIn", value="空箱是否入库(YES,NO)",paramType="query",dataType="string",allowableValues = "YES,NO,ALL"),
            @ApiImplicitParam(name="account", value="是否已入账(YES,NO)",paramType="query",dataType="string",allowableValues = "YES,NO,ALL"),
            @ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
            @ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
    })
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Map> list(HttpServletRequest request, @ApiIgnore @Validated ClientRepaymentList listDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String dealerId = dUserInfo.getdId();
        listDto.setPage((listDto.getPage()-1)*10);
        Map map = cOrderBaseService.queryClientRepaymentList(dealerId,listDto);
        int count = cOrderBaseService.queryClientRepaymentListCount(dealerId,listDto);
        map.put("count",count);
/*        PagingData pagingData = new PagingData<>();
        pagingData.setData(map);
        pagingData.setRecordsTotal(count);*/
        return new ClientRepaymentExcution<Map>(map, ClientRepaymentEnum.SUCCESS);
    }


    @ApiOperation(value = "确认回款", nickname = "clientRepaymentConfirm", notes = "", tags={ "@石匡" })
    @PutMapping(value = "confirm",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> confirm(HttpServletRequest request, @Validated @RequestBody ClientRepaymentConfirm clientRepaymentConfirm) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String dealerId = dUserInfo.getdId();
        String userId = dUserInfo.getId();
        int num = cOrderBaseService.clientRepaymentConfirm(userId,dealerId,clientRepaymentConfirm);
        if (num <1){
            return new ClientRepaymentExcution<Void>(null, ClientRepaymentEnum.FAILURE);
        }
        return new ClientRepaymentExcution<Void>(null, ClientRepaymentEnum.SUCCESS);
    }

    @ApiOperation(value = "批量确认回款", nickname = "clientRepaymentPiConfirm", notes = "", tags={ "@石匡" })
    @PutMapping(value = "piConfirm",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> piConfirm(HttpServletRequest request, @Validated @RequestBody piComfirm piComfirm) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String dealerId = dUserInfo.getdId();
        String userId = dUserInfo.getId();
        int num = cOrderBaseService.clientRepaymentPiConfirm(userId,dealerId,piComfirm);
        if (num <1){
            return new ClientRepaymentExcution<Void>(null, ClientRepaymentEnum.FAILURE);
        }
        return new ClientRepaymentExcution<Void>(null, ClientRepaymentEnum.SUCCESS);
    }


    @ApiOperation(value = "客户回款单详情", nickname = "details", notes = "", tags={ "@石匡" })
    @ApiImplicitParams({
            @ApiImplicitParam(name="cid", value="客户ID",paramType="query",dataType="string"),
            @ApiImplicitParam(name="id", value="订单ID",paramType="query",dataType="string")
    })
    @GetMapping(value = "details",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<ClientRepaymentDetailsVo> details(HttpServletRequest request, @ApiIgnore @Validated ClientRepaymentDetails details) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String dealerId = dUserInfo.getdId();
        ClientRepaymentDetailsVo detail = cOrderBaseService.queryClientRepaymentDetails(dealerId,details);
        return new ClientRepaymentExcution<ClientRepaymentDetailsVo>(detail, ClientRepaymentEnum.SUCCESS);
    }

}
